<!--#include file="./header.inc"-->
<%

select case aq
	'view
	case "edit" 	: 	call edit
	case "save" 	: 	call save
	case "delete" 	: 	call delete
	case "update" 	: 	call update
	case else 		: 	call index
end select

call db.close()

function index()
	dim keyword
	keyword = admin.filter(request("keyword"))
	sql = "select id,viewName,viewContent,isSystem from cj_view"
	if keyword <> "" then sql = sql & " where viewContent like '%" & keyword & "%'"
	sql = sql & " Order By id desc"
	temp = db.arrayData(sql, page, rows)
	infoTotal = temp(0)
	data = temp(1)
	%><!--#include file="./view/view.index.html"--><%
end function

function edit()
	dim content
	content = "<!doctype html><html><head><meta charset=""utf-8"" />" & vbCr &_
		"<title>{$C(siteName)}</title>" & vbCr &_
		"<meta name=""keywords"" content=""{$C(metaKeywords)},{$C(siteName)}"" />" & vbCr &_
		"<meta name=""description"" content=""{$C(metaDescription)},{$C(siteName)}"" />" & vbCr &_
		"<link href=""{$siteUrl}view/style/style.css"" rel=""stylesheet"">" & vbCr &_
		"<meta name=""renderer"" content=""webkit"" />" & vbCr &_
		"</head><body>" & vbCr & vbCr & "内容" & vbCr & vbCr & "</body></html>"
	data = array("", content, 0)
	if iid > 0 then
		set rs = db.exec("select viewName,viewContent,isStop from cj_view where id=" & iid,1)
		if rs.eof then admin.message "指定的模板ID不存在."
		data = rs
		set rs = nothing
	end if
	%><!--#include file="./view/view.edit.html"--><%
end function

function save()
	viewName = admin.filter(request.form("viewName"))
	viewContent = trim(request.form("viewContent"))
	if viewName = "" then admin.message "模板名称不能空！"
	if len(viewContent) < 10 then admin.message "模板内容不能太少！"
	if iid > 0 then
		set rs = db.exec("select * from cj_view where id=" & iid, 3)
		if rs.eof then admin.message "id不存在！"
	else
		set rs = db.exec("select * from cj_view", 3)
		rs.AddNew
	end if
	rs("viewName") = viewName
	rs("viewContent") = viewContent
	rs.update
	rs.close : set rs = nothing
	if iid > 0 then
		call app.createView(iid)
		admin.back "修改成功！", "返回列表", "?q=index"
	else
		iid = admin.queryTable("top 1 id", "view Order by id desc")
		call app.createView(iid)
		admin.back "添加成功！", "继续添加", "?q=edit"
	end if
end function

function delete()
	sql = "select count(classViewID),"
	sql = sql & "count(contentViewID),"
	sql = sql & "(select count(genreViewID) from cj_genre where genreViewID in(" & batchId & ")),"
	sql = sql & "(select count(infoViewID) from cj_news where infoViewID in(" & batchId & ")),"
	sql = sql & "(select count(infoViewID) from cj_product where infoViewID in(" & batchId & ")),"
	sql = sql & "(select count(infoViewID) from cj_our where infoViewID in(" & batchId & "))"
	sql = sql & " from cj_class where classViewID in(" & batchId & ") or contentViewID in(" & batchId & ")"
	set rs = db.exec(sql, 1)
	if rs(0) > 0 or rs(1) > 0 or rs(2) > 0 or rs(3) > 0 or rs(4) > 0 or rs(5) > 0 then
		app.json "正在使用的模板不可以删除！",1
	end if
	call db.exec("Delete from cj_view where id in(" & batchId & ") and isSystem=false", 1)
	call app.fileCacheDelete()
	call app.createView(0)
	app.json "删除成功！",0
end function

function update()
	if app.createView(iid) then
		admin.message "生成成功！"
	else
		admin.message "id不存在！"
	end if
end function

%>